﻿#include "NCTDialogMultipleOwned.h"
#include "ui_NCTDialogMultipleOwned.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTLayUp.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTMultipleNoise.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTUserDefined.h"
#include <QMessageBox>

namespace GUI
{
    NCTDialogMultipleOwned::NCTDialogMultipleOwned(Acoustics::FITKAcousticsNCTMultipleNoiseWeight *data)
        :AbstractMaterialDialog(Create, nullptr), _data(data)
    {
        _ui = new Ui::NCTDialogMultipleOwned;
        _ui->setupUi(this);
      
        this->initComboBox();
        this->init();
        connect(_ui->comboBox_type, SIGNAL(currentIndexChanged(QString)), this, SLOT(slot_comboBox_type_currentText(QString)));
    }

    NCTDialogMultipleOwned::~NCTDialogMultipleOwned()
    {
        if (_ui) delete _ui;
    }

    Acoustics::FITKAcousticsNCTMultipleNoiseWeight * NCTDialogMultipleOwned::getData()
    {
        return _data;
    }

    void NCTDialogMultipleOwned::init()
    {
        if (!_nctMgr||!_data)return;
        _ui->lineEdit_coverage->setText(QString::number(_data->getCoverage() * 100));

        Acoustics::FITKAcousticsAbsNCT* data = _nctMgr->getDataByID(_data->getNCTID());
        if (!data) {
            _ui->comboBox_type->setCurrentIndex(-1);//0是"Layup"
            _ui->stackedWidget->setCurrentIndex(0);
            _ui->comboBox_treatment->setCurrentIndex(-1);
            return;
        }
        int ID = _data->getDataObjectID();
        int index1 = _ui->comboBox_treatment->findData(ID);

        if(data->getNCTType()== Acoustics::FITKAcousticsAbsNCT::NCTType::TreatmentLayUp){
            _ui->comboBox_type->setCurrentIndex(0);//0是"Layup"
            _ui->stackedWidget->setCurrentIndex(0);
            _ui->comboBox_treatment->setCurrentIndex(index1);
        }
        else if (data->getNCTType() == Acoustics::FITKAcousticsAbsNCT::NCTType::UserDefTreatment) {
            _ui->comboBox_type->setCurrentIndex(1);//1是"User-defined Treatment"
            _ui->stackedWidget->setCurrentIndex(1);
            _ui->comboBox_treatment2->setCurrentIndex(index1);
        }
    }

    void NCTDialogMultipleOwned::initComboBox()
    {
        if (!_nctMgr)return;
        _ui->comboBox_type->addItems({ "Layup","User-defined Treatment" });
        QList<Acoustics::FITKAcousticsAbsNCT*> layup = _nctMgr->getAcousticsNCTByType(Acoustics::FITKAcousticsAbsNCT::TreatmentLayUp);
        for (auto data : layup) {
            _ui->comboBox_treatment->addItem(data->getDataObjectName(), data->getDataObjectID());
        }
        QList<Acoustics::FITKAcousticsAbsNCT*> ud = _nctMgr->getAcousticsNCTByType(Acoustics::FITKAcousticsAbsNCT::UserDefTreatment);
        for (auto data : ud) {
            _ui->comboBox_treatment2->addItem(data->getDataObjectName(), data->getDataObjectID());
        }
    }

    bool NCTDialogMultipleOwned::checkInput()
    {
        QString text=_ui->lineEdit_coverage->text().trimmed();
        bool ok = false;
        if (text != "")text.toDouble(&ok);
        if (!ok){
            QMessageBox::critical(nullptr, tr("Critical"), tr("Please enter a number."));
            return false;
        }
        return true;
    }

    void NCTDialogMultipleOwned::on_pushButton_OK_clicked()
    {
        if (!this->checkInput()) return;
        if (_data == nullptr) {
            _data = new Acoustics::FITKAcousticsNCTMultipleNoiseWeight();
        }
        if (_ui->comboBox_type->currentIndex() == 0)
            _data->setNCTID(_ui->comboBox_treatment->currentData().toInt());
        else
            _data->setNCTID(_ui->comboBox_treatment2->currentData().toInt());
        _data->setCoverage(_ui->lineEdit_coverage->text().toDouble()/100);
        
        this->accept();
    }

    void NCTDialogMultipleOwned::on_pushButton_Cancel_clicked()
    {
        this->reject();
    }

    void NCTDialogMultipleOwned::slot_comboBox_type_currentText(QString str)
    {
        if (str == "Layup") {
            _ui->stackedWidget->setCurrentIndex(0);
            if(_ui->comboBox_treatment->currentText().isEmpty())
                _ui->comboBox_treatment->setCurrentIndex(0);
        }
        else {
            _ui->stackedWidget->setCurrentIndex(1);
            if (_ui->comboBox_treatment2->currentText().isEmpty())
                _ui->comboBox_treatment2->setCurrentIndex(0);
        }
    }

}